/* Quiz Manager - Create and edit web quizzes.
 * Copyright (C) 2008  Alexis Brunet, Jean-Michel Brunet, Enrique Hotes, Carlo Russo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package soen343.data;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.dsrg.ts.threadLocal.DbRegistry;

public class ShortAnswerQuestionTDG {
	public static ResultSet findById(int id) throws SQLException{
		String SQLString = 	"SELECT * FROM short_answer WHERE questionid = ?";
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SQLString);
		ps.setInt(1, id);
		return ps.executeQuery();
	}

	public static void insert(int questionId, String text) throws SQLException{
		String sqlString = "INSERT INTO short_answer (questionid, choiceid, text) VALUES (?, 1, ?)";
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(sqlString);
		
		ps.setInt(1, questionId);
		ps.setString(2, text);
		
		ps.executeUpdate();
	}
	
	public static void update(int questionId, String text) throws SQLException{
		String sqlString = "UPDATE short_answer SET text = ? WHERE questionid = ?";
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(sqlString);
		
		ps.setString(1, text);
		ps.setInt(2, questionId);
		
		ps.executeUpdate();
	}
	
	public static void delete(int questionId) throws SQLException{
		String sqlString = "DELETE FROM short_answer WHERE questionid = ?";
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(sqlString);

		ps.setInt(1, questionId);
		
		ps.executeUpdate();
	}
	
}
